package com.example.qjia.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.qjia.Presenters.DetailHolidayMessagePresenter;
import com.example.qjia.Presenters.DetailHolidayMessagePresenterImp;
import com.example.qjia.R;
import com.example.qjia.basic.BaseActivity;
import com.example.qjia.data.HolidayMessage;
import com.example.qjia.data.romotedata.AbsenceSlip;
import com.example.qjia.ui.ViewInterface.DetailHolidayMessageInterface;

import java.text.SimpleDateFormat;

public class HolidayDetailActivity extends BaseActivity implements DetailHolidayMessageInterface, View.OnClickListener {
    private int aid;
    private static final String TAG="HolidayDetailActivity";
    private LinearLayout back_ly;
    private LinearLayout erweiMap_ly;
    private RelativeLayout holidayMessage_ly;
    private TextView studentName;
    private TextView schoolSub;//学院
    private TextView studentClass;
    private TextView requestTime;
    private TextView holidayRequestTime;
    private TextView holidayReason;
    private TextView checkState;
    private TextView checkMan;
    private RelativeLayout loading_ly;
    private TextView errorReload;
    private DetailHolidayMessagePresenter detailHolidayMessagePresenter;
    private String[] states;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_holiday_detail;
    }

    @Override
    protected void init() {
        states=new String[]{
                "未审核","已通过","不批准"
        };
        aid=getIntent().getIntExtra("holidayId",0);
        Log.i(TAG, "init: "+aid);
        back_ly=(LinearLayout)findViewById(R.id.back_ly);
        back_ly.setOnClickListener(this);
        erweiMap_ly=(LinearLayout)findViewById(R.id.erweiMap_ly);
        erweiMap_ly.setOnClickListener(this);
        holidayMessage_ly=(RelativeLayout)findViewById(R.id.holiday_message_ly);
        studentName=(TextView)findViewById(R.id.student_name);
        schoolSub=(TextView)findViewById(R.id.school_sub);
        studentClass=(TextView)findViewById(R.id.student_class);
        requestTime=(TextView)findViewById(R.id.request_time);
        holidayRequestTime=(TextView)findViewById(R.id.holiday_request_time);
        holidayReason=(TextView)findViewById(R.id.request_reason);
        checkState=(TextView)findViewById(R.id.holiday_check_state);
        checkMan=(TextView)findViewById(R.id.holiday_check_man);
        loading_ly=(RelativeLayout)findViewById(R.id.loading_ly);
        errorReload=(TextView)findViewById(R.id.load_error);
        errorReload.setOnClickListener(this);
        detailHolidayMessagePresenter=new DetailHolidayMessagePresenterImp(this);


    }

    @Override
    protected void initView() {
        detailHolidayMessagePresenter.loadHolidayMessage(aid+"");

    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.back_ly)detailHolidayMessagePresenter.backTomain();
        if(view.getId()==R.id.erweiMap_ly)detailHolidayMessagePresenter.goToMap();
        if(view.getId()==R.id.load_error)detailHolidayMessagePresenter.loadHolidayMessage(aid+"");

    }

    @Override
    public void showProgressBar() {
        loading_ly.setVisibility(View.VISIBLE);

    }

    @Override
    public void hideProgressBar() {
        loading_ly.setVisibility(View.GONE);
    }

    @Override
    public void setHolidayMessage(AbsenceSlip holidayMessage) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm");
        String startTime=simpleDateFormat.format(holidayMessage.getTimeStart());//请假的开始的时间
        String endTime= simpleDateFormat.format(holidayMessage.getTimeEnd());
        Log.i(TAG, "setHolidayMessage: "+holidayMessage.getStatus());
        holidayMessage_ly.setVisibility(View.VISIBLE);
        studentName.setText(holidayMessage.getStudent().getName());
        schoolSub.setText(holidayMessage.getStudent().getAcademy());
        studentClass.setText(holidayMessage.getStudent().getMclass());
        requestTime.setText(startTime);
        holidayRequestTime.setText(endTime);
        holidayReason.setText(holidayMessage.getReason());
        checkState.setText(states[holidayMessage.getStatus()]);
        checkMan.setText("("+holidayMessage.getTeacher().getName()+")");


    }

    @Override
    public void errorReload() {
        holidayMessage_ly.setVisibility(View.INVISIBLE);
        errorReload.setVisibility(View.VISIBLE);

    }

    @Override
    public void backToMain() {
        finish();

    }

    @Override
    public void goToBimap() {
        Intent intent=new Intent(HolidayDetailActivity.this,ErWeiMapActivity.class);
        startActivity(intent);

    }
}
